{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "791038a8",
   "metadata": {},
   "source": [
    "---\n",
    "* 2023-10-23\n",
    "* 课程：Python语言\n",
    "* week08课堂\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24b79194",
   "metadata": {},
   "source": [
    "\n",
    "# 本周内容\n",
    "\n",
    "## 1. 列表进阶\n",
    "\n",
    "## 2. 课本项目\n",
    "\n",
    "## 3. 留一个课堂项目给大家做\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75f2a93a",
   "metadata": {},
   "source": [
    "### 1.1 接上周内容（） 列表的方法--remove\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3d754d0c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李四', '小明', 'Jalex']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list = [\"张三\",\"李四\",\"小明\",\"Jalex\"]\n",
    "\n",
    "name_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3030aada",
   "metadata": {},
   "source": [
    "* 思考：我怎么知道‘李鹏’在数据列表里面呢？\n",
    "\n",
    "> 1. 先查询\n",
    "> 2. 后删除\n",
    "\n",
    "* in 方法(查询元素的方法)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "6f2a6f4f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 列表的循环\n",
    "for i in name_list:\n",
    "    if i == \"张三\":\n",
    "        print(i)\n",
    "        name_list.remove(\"张三\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "64519625",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['李四', 'Jalex']"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "11409e3e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"小明\" in name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "4f438475",
   "metadata": {},
   "outputs": [],
   "source": [
    "if \"小明\" in name_list:\n",
    "    name_list.remove(\"小明\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "62aef8c8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['李四', 'Jalex']"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2d0cee97",
   "metadata": {},
   "source": [
    "### 1.2 列表的其他方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "4333475d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['李四', 'Jalex']"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# clear 清空 ，清除列表中的所有元素\n",
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "3c878e3f",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list.clear()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "9cbb7e81",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "0450a306",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李四', '小明', 'Jalex', '李四']"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# count 查看list中统计元素的数量\n",
    "name_list = [\"张三\",\"李四\",\"小明\",\"Jalex\",\"李四\"]\n",
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "dec80aed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list.count(\"李四\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "49aff0a4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 案例\n",
    "# 假设掷骰子100次，统计1-6各出现多少次\n",
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "def474ce",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 是不是需要现有一个空列表？来存放我们产生的数据呢？\n",
    "random_list = []\n",
    "# 增加数据到列表里面？\n",
    "for i in range(100):\n",
    "    random_list.append(random.randint(1,6))\n",
    "#     print(random.randint(1,6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "386d62bb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 6, 3, 2, 4, 3, 1, 1, 3, 4, 1, 4, 5, 5, 2, 2, 4, 2, 2, 2, 4, 1, 3, 4, 5, 6, 3, 3, 3, 6, 5, 4, 5, 5, 6, 4, 3, 4, 6, 6, 2, 1, 3, 4, 4, 5, 5, 1, 3, 2, 3, 4, 5, 4, 6, 2, 1, 2, 1, 3, 4, 1, 2, 6, 3, 2, 1, 4, 6, 2, 3, 3, 2, 5, 6, 3, 2, 3, 3, 2, 6, 2, 3, 2, 1, 2, 3, 4, 4, 5, 6, 2, 1, 4, 3, 6, 1, 6, 2]\n"
     ]
    }
   ],
   "source": [
    "print(random_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "3bae58fb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 14 0.14\n",
      "2 22 0.22\n",
      "3 21 0.21\n",
      "4 18 0.18\n",
      "5 11 0.11\n",
      "6 14 0.14\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,7):\n",
    "    print(i,random_list.count(i),random_list.count(i)/100)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c241f03e",
   "metadata": {},
   "source": [
    "### 1.3 列表元素的排序 sort() 和reverse()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "1afcbb0e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李四', '小明', 'Jalex', '李四']"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list = [\"张三\",\"李四\",\"小明\",\"Jalex\",\"李四\"]\n",
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "59920ebc",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list.sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "46edbc28",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jalex', '小明', '张三', '李四', '李四']"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "741aba2c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[20, 18, 19, 21, 22, 30, 17]"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list = [20,18,19,21,22,30,17]\n",
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "1e618a78",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[17, 18, 19, 20, 21, 22, 30]"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list.sort()\n",
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "005de62e",
   "metadata": {},
   "outputs": [],
   "source": [
    "age_list.reverse()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "ab0d114e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[30, 22, 21, 20, 19, 18, 17]"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d9eab46b",
   "metadata": {},
   "source": [
    "### 1.4 copy()\n",
    "\n",
    "> 1. =\n",
    "> 2. copy()\n",
    "> 3. deep copy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "449ebc17",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2127066658368\n",
      "2127066275152\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['张三', '李四', '小明', 'Jalex', '李四']"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list01 = [\"张三\",\"李四\",\"小明\",\"Jalex\",\"李四\"]\n",
    "print(id(name_list01))\n",
    "print(id(name_list01[0]))\n",
    "name_list01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "2178fa0f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2127066658368\n",
      "2127066275152\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['张三', '李四', '小明', 'Jalex', '李四']"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list02 = name_list01\n",
    "print(id(name_list02))\n",
    "print(id(name_list02[0]))\n",
    "name_list02"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "4a127dfe",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list01.append('Marry')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "8a706209",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李四', '小明', 'Jalex', '李四', 'Marry']"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "4196107d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李四', '小明', 'Jalex', '李四', 'Marry']"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list02"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11477d87",
   "metadata": {},
   "source": [
    "* 如上原因：我们不得不找一个方法来实现“真正的”复制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "73efa24e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2127041269824\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['张三', '李四', '小明', 'Jalex', '李四']"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list01 = [\"张三\",\"李四\",\"小明\",\"Jalex\",\"李四\"]\n",
    "print(id(name_list01))\n",
    "name_list01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "3148e168",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2127062426752\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['张三', '李四', '小明', 'Jalex', '李四']"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list02 = name_list01.copy()\n",
    "print(id(name_list02))\n",
    "name_list02"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "19ca11b7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李四'"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list01.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "36ca80ea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李四', '小明', 'Jalex']"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "b53e4cff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李四', '小明', 'Jalex', '李四']"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list02"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d31cd489",
   "metadata": {},
   "source": [
    "# 2. 课后实践\n",
    "## 2.1 实践：购物车项目"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "399cce38",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "购物车程序项目：\n",
    "\n",
    "> 1. 运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "> 2. 让用户输入商品编号进行商品的购买。\n",
    "> 3. 用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。\n",
    "> 4. 用户可以随时退出购买，推出时打印用户已购买的商品和支付宝余额。\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "753a2952",
   "metadata": {},
   "source": [
    "```\n",
    "# 1. \n",
    "Alipay = input()\n",
    "商品列表 = [\n",
    "    \"内容\"\n",
    "]\n",
    "\n",
    "print(商品列表)\n",
    "购物车列表 = [\n",
    "\n",
    "]\n",
    "\n",
    "# 2. # 3.\n",
    "users_choese = input()\n",
    "if 钱包余额 > 商品价格:\n",
    "    扣钱，加商品进购物车列表（append）\n",
    "else\n",
    "    print(\"你的钱不够，......\")    \n",
    "\n",
    "# 4. \n",
    "break 退出循环，打印购物车列表\n",
    "\n",
    "```\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3867aa7f",
   "metadata": {},
   "source": [
    "## 2.2 实践：课本第二章列表的项目"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "276282d1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
